package com.mall.user.services;


import com.mall.user.IloginServices;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.converter.UserConverterMapper;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dto.CheckAuthRequest;
import com.mall.user.dto.CheckAuthResponse;
import com.mall.user.dto.UserLoginRequest;
import com.mall.user.dto.UserLoginResponse;
import com.mall.user.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Component
@Slf4j
public class IloginServiceImpl implements IloginServices {
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    UserConverterMapper userConverterMapper;
    @Override
    public UserLoginResponse login(UserLoginRequest loginRequest) {
        loginRequest.requestCheck();//判空

        //密码需要加密
        String password = DigestUtils.md5DigestAsHex(loginRequest.getPassword().getBytes());

        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username", loginRequest.getUserName())
                .andEqualTo("password", password);
        List<Member> members = memberMapper.selectByExample(example);

        if(CollectionUtils.isEmpty(members)){//用户名或密码不正确
            UserLoginResponse response = new UserLoginResponse();
            response.setCode(SysRetCodeConstants.USERORPASSWORD_ERRROR.getCode());
            response.setMsg(SysRetCodeConstants.USERORPASSWORD_ERRROR.getMessage());
            return response;
        }
        Member member = members.get(0);
        UserLoginResponse response = userConverterMapper.converter(member);
        response.setCode(SysRetCodeConstants.SUCCESS.getCode());
        response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());

        // response.setToken(member1.getPassword());
        //此处设置了token的获取方式
        //todo
        String token = JwtTokenUtils.builder().msg(member.getId().toString()).build().creatJwtToken();
        response.setToken(token);
        return response;
    }

    @Override
    public CheckAuthResponse validToken(CheckAuthRequest checkAuthRequest) {
        CheckAuthResponse checkAuthResponse = new CheckAuthResponse();
        try {
            String userInfo = JwtTokenUtils.builder().token(checkAuthRequest.getToken()).build().freeJwt();
            checkAuthResponse.setUserinfo(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            checkAuthResponse.setCode(SysRetCodeConstants.TOKEN_VALID_FAILED.getCode());
            checkAuthResponse.setCode(SysRetCodeConstants.TOKEN_VALID_FAILED.getMessage());
            return checkAuthResponse;
        }
        checkAuthResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        checkAuthResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return checkAuthResponse;
    }

}
